#import "CTRefuseView.h"
#import "CTRefuseBottomView.h"
#import "CTRefuseCell.h"
#import "CTRefuseHeader.h"
#import "CTRefuseModel.h"
#import "BYTextView.h"
#import "CTKeyboardInputView.h"
@interface CTRefuseView () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) CTRefuseBottomView *footerView;
@property (nonatomic ,strong) CTRefuseModel *selectSectionModel;
@property (nonatomic ,strong) CTRefuseModel *selectRowModel;
@property (nonatomic ,strong) BYTextView *textView;
@end
@implementation CTRefuseView
- (void)initSubViews
{
    self.shadowView.frame = CGRectMake(0, self.bounds.size.height,self.bounds.size.width, Ratio(520)+[UIDevice ct_safeBottom]);
    self.contentView.frame = self.shadowView.bounds;
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.titleLabel.textColor = [UIColor colorFF6C2E];
    self.titleLabel.font = [UIFont systemFontOfSize:Ratio(18)];
    self.doneButton.hidden = YES;
    self.backgroundView.alpha = 0.01;
    self.titleLabel.text = @"Reasons to opt out";
    [self.contentView addSubview:self.tableView];
    [self.contentView addSubview:self.footerView];
    self.selectSectionModel = [[CTRefuseModel alloc] init];
    self.selectRowModel = [[CTRefuseModel alloc] init];
}
#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CTRefuseModel *model = self.dataArray[section];
    return model.open ? model.secondaryLevelData.count : 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CTRefuseModel *model = self.dataArray[indexPath.section];
    CTRefuseModel *rowModel = model.secondaryLevelData[indexPath.row];
    CTRefuseCell *cell = [CTRefuseCell cellForTableView:tableView];
    cell.titleLabel.text = rowModel.templateCustomName;
    cell.isSelect = rowModel.open;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CTRefuseModel *model = self.dataArray[indexPath.section];
    CTRefuseModel *secondModel = model.secondaryLevelData[indexPath.row];
    return secondModel.cellHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CTRefuseModel *model = self.dataArray[section];
    CTRefuseHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CTRefuseHeader"];
    header.titleLabel.text = model.templateCustomName;
    header.isSelect = model.open;
    __weak typeof(self) weakself = self;
    header.tapClick = ^{
        model.open = !model.open;
        weakself.selectSectionModel.open = NO;
        weakself.selectRowModel.open = NO;
        weakself.selectSectionModel = model;
        [weakself.tableView reloadData];
        if (section == weakself.dataArray.count-1) {
        }
    };
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CTRefuseModel *model = self.dataArray[section];
    return model.cellHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    CTRefuseModel *model = self.dataArray[section];
    if (section == self.dataArray.count-1 && model.open) {
        return self.textView;
    } else {
        return [UIView new];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CTRefuseModel *model = self.dataArray[section];
    if (section == self.dataArray.count-1 && model.open) {
        return Ratio(80);
    } else {
        return 0.01;
    }
}
- (BYTextView *)textView {
    if (!_textView) {
        _textView = [[BYTextView alloc] init];
        _textView.contentPlaceHolder = @"Please fill in the reason";
        _textView.contentText = @"";
        _textView.maxInputLength = 1000;
        _textView.countLabel.hidden = YES;
        __weak typeof(self) weakSelf = self;
        _textView.inputTextComplete = ^(NSString * _Nonnull content) {
            weakSelf.selectRowModel.causeDescription = content;
        };
    }
    return _textView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CTRefuseModel *model = self.dataArray[indexPath.section];
    CTRefuseModel *rowModel = model.secondaryLevelData[indexPath.row];
    rowModel.open = !rowModel.open;
    self.selectRowModel.open = NO;
    self.selectRowModel = rowModel;
    [self.tableView reloadData];
}
#pragma mark - Action
- (void)dismissPicker {
    [self dismiss];
}
- (void)clickDoneBtn {
    if (beNil(self.selectSectionModel.templateCustomName)) {
        [self showText:@"Please select a reason"];
        return;
    }
    [self showLoading];
    NSDictionary *paramDic = @{
        @"userGroup" : returnBeNil([CTUserManager shareManager].userModel.userGroup),
        @"oneLevel" : @(self.selectSectionModel.uniqueRecordIdentifier),
        @"twoLevel" : @(self.selectRowModel.uniqueRecordIdentifier),
        @"reason" : returnBeNil(self.selectRowModel.causeDescription),
        @"createTime" : kGetCurentTime,
        @"bizType" : @(0),
    };
    [CTBaseRequest postRequestWithMethodName:CTPath(@"/saveUserOptions", CTPathTypePost, nil) parametersDic:paramDic success:^(CTBaseRequest * _Nonnull operation, BOOL isSuccess, id  _Nonnull responseObject) {
        [self dismissLoading];
        if (!isSuccess) {
            [self showInfoText:(NSString *)responseObject];
        } else {
            [self dismiss];
            if (self.reasonSelected) {
                self.reasonSelected(@"", YES);
            }
        }
    }];
}
#pragma mark - Getter
- (UITableView *)tableView
{
    if (_tableView==nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, Ratio(50), kScreenWidth, self.shadowView.ct_height-Ratio(120)-[UIDevice ct_safeBottom]) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[CTRefuseHeader class] forHeaderFooterViewReuseIdentifier:@"CTRefuseHeader"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
        }
    }
    return _tableView;
}
- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    [self.tableView reloadData];
}
- (CTRefuseBottomView *)footerView {
    if (!_footerView) {
        _footerView = [[CTRefuseBottomView alloc] initWithFrame:CGRectMake(0, self.shadowView.ct_height-Ratio(70)-[UIDevice ct_safeBottom], kScreenWidth, Ratio(70)+[UIDevice ct_safeBottom])];
        _footerView.backgroundColor = [UIColor whiteColor];
        [_footerView.confirmButton addTarget:self action:@selector(dismissPicker) forControlEvents:UIControlEventTouchUpInside];
        [_footerView.cancleButton addTarget:self action:@selector(clickDoneBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _footerView;
}
@end
